<?php
@session_start();
include_once("../initial.php");
$DOCUMENT_ROOT=$_SERVER["DOCUMENT_ROOT"];
include_once(constant('PROJECT_DIR')."/logic/deviceLogic.php");
include_once(constant('PROJECT_DIR')."/logic/categoryLogic.php");
include_once(constant('PROJECT_DIR')."/logic/deviceLogic.php");
$LIST_NUMBER_PER_PAGE=10;
//begin to read device
require_once(constant('PROJECT_DIR')."/checkLogin.php");
require_once(constant('PROJECT_DIR')."/checkAdminLogin.php");
$adminId=$_SESSION['user'];

$dl = new deviceLogic();
$cl = new categoryLogic();
if(!isset($_POST['action']))
{
     $action="";
}
else
{
     $action = $_POST['action'];
}
if($action == "")
{
		if(isset($_GET['action']))
		{
			$action=$_GET['action'];
		}
}
$searchResult=array();
$searchNote="";
switch($action)
{
     case "addcategory":
         $name=$_POST['name'];
         if($name=='')
         {
         		$smarty->assign("addcategoryNotice","请输入名字");
         		break;
         }
         $info=$_POST['info'];
         $category=array();
         $category["name"]=$name;
         $category["info"]=$info;
         $category["createtime"]=date("Y-m-d H:i:s");
         $cl->insert_category($category);
         break;
     case "deletecategory":
     			if(!isset($_POST['category']))
     			{
     				break;
     				}
         $categories=$_POST['category'];
         for($i=0; $i<sizeof($categories); $i++)
         {
             $cl->delete_category($categories[$i]);
         }
         break;
     case "modifycategory":
         $uid=$_POST['categoryid'];
         $category=array();
         $category["id"]=$_POST["categoryid"];
         $category["name"]=$_POST["name"];
         $category["info"]=$_POST["info"];
         $cl->update_category($category, $uid);
         break;
     case "addCategoryDevice":
         $categoryDevice=$_POST["deviceid"];
         $categoryid=$_POST["categoryid"];
         $barcode=$_POST["barcode"];
         $createtime=date("Y-m-d H:i:s");
         if($categoryDevice<0)
         {
         		//去按照barcode读取设备，再获取设备id
         		$deviceTmp=$dl->get_device(array("barcode"=>$barcode));
         		if(count($deviceTmp)<=0)
         		{
         				$addCategoryDeviceNotice="请选择设备或者输入设备码";
         				$smarty->assign("addCategoryDeviceNotice", $addCategoryDeviceNotice);
         				break;
         		}
         		else
         		{
         			$categoryDevice=$deviceTmp[0]['id'];
         		}
         	}
         $existNumber=$cl->get_category_device_number($categoryid, $categoryDevice);
         if($existNumber>0)
         {
         		$addCategoryDeviceNotice="该用户与该课程对应关系已经存在";
         		$smarty->assign("addCategoryDeviceNotice", $addCategoryDeviceNotice);
         }
         else
         {
         		$cl->insert_category_device($categoryid, $categoryDevice, $createtime);
         }
         break;
     case "saveCategoryCourseBooktime":
     		$uid=$_POST['categoryId'];
     		$cid=$_POST['deviceId'];
     		$booktime=$_POST['booktime'];
     		if($booktime<0)
     		{
     			break;
     		}
     		$cl->update_category_device_booktime($uid, $cid, $booktime);
     		break;
    case "removecategorydevice":
    		$cid=$_GET['id'];
     		$did=$_GET['deviceid'];
     		$cl->delete_category_device($cid, $did);
     		break;
    case "search":
				$keyword = $_GET['keyword'];
    		if(strlen($keyword)<3)
    		{
    				$searchNote .="输入的关键词太短，不进行搜索,请输入长度超过3的字符串.";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		//keyword长度要超过2
    		$searchResult = $cl->searchByKeyword($keyword, "limit 0,30");//默认显示
    		if(count($searchResult)==30)
    		{
    				$searchNote .="搜索词得到太多结果，只显示30条，请输入更长的搜索词";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		if(count($searchResult)==0)
    		{
    				$searchNote .="没有结果，展示所有结果";
    				$smarty->assign("searchNote", $searchNote);
    		}
    		
    		$smarty->assign("keyword", $keyword);
    		break;
}
if(isset($_GET['id']))
{
     //get the editting category info
     $editCategory = $cl->get_category(array("id"=>$_GET["id"]));
     $smarty->assign("editCategory", $editCategory[0]);
     
     //读取所有设备
     $deviceLg = new deviceLogic();
     $devices= $deviceLg->get_device("","order by id desc", "");
     
     
     //读取分类相关的设备
     $categoryDevices=$cl->get_category_devices($editCategory[0]['id']);
     
     //筛选所选择的设备
     $filterDevices=array();
     for($i=0; $i<count($devices); $i++)
     {
     		$contains=0;
     		for($j=0; $j<count($categoryDevices); $j++)
     		{
     				if($categoryDevices[$j]['did'] == $devices[$i]['id'])
     				{
     						$contains=1;
     						break;
     				}
     		}
     		if($contains==0)
     		{
     			array_push($filterDevices, $devices[$i]);
     		}
     	}
     	$smarty->assign("devices", $filterDevices);
     	
     $deviceId2Name=array();
     if(count($categoryDevices)>0)
     {
     		for($i=0; $i< count($devices); $i++)
     		{
     				$deviceId2Name[$devices[$i]['id']]=$devices[$i]['name'];
     			}
     	}
     for($i=0; $i < count($categoryDevices); $i++)
     {
     		$categoryDevices[$i]['deviceName']=$deviceId2Name[$categoryDevices[$i]['did']];
     }
     if(count($categoryDevices))
     {
     			$smarty->assign("categoryDevices", $categoryDevices);
     	}
     	
}


//handling the page index
$categorysNumber=0;
if(count($searchResult)>0)
{
		$categorysNumber=count($searchResult);
}
else
{
	$categorysNumber=$cl->get_category_num();
}

$pageIndex=1;
$totalPageNumber=ceil($categorysNumber/$LIST_NUMBER_PER_PAGE);
if(isset($_GET['pageIndex']))
{
     $pageIndex=$_GET['pageIndex'];
     if($pageIndex<1 || $pageIndex > $totalPageNumber)
     {
         $pageIndex=1;
     }
}
$startItem=($pageIndex-1)*$LIST_NUMBER_PER_PAGE;
$ItemNumber=$LIST_NUMBER_PER_PAGE;

$leftPageIndex=$pageIndex<=1?1:$pageIndex-1;
$rightPageIndex=$pageIndex>=$totalPageNumber?$totalPageNumber:$pageIndex+1;
$leftMostPageIndex=$pageIndex<=5?1:$pageIndex-5;//左边显示5个页面索引
$rightMostPageIndex=$totalPageNumber<$pageIndex+5?$totalPageNumber:$pageIndex+5;//右边显示5个页面索引
$smarty->assign("leftMostPageIndex",$leftMostPageIndex);
$smarty->assign("rightMostPageIndex",$rightMostPageIndex);
$smarty->assign("leftPageIndex",$leftPageIndex);
$smarty->assign("rightPageIndex",$rightPageIndex);
$smarty->assign("pageIndex",$pageIndex);
$smarty->assign("categorysNumber", $categorysNumber);
$allcategorys=array();
if(count($searchResult)>0)
{
		$allcategorys=$searchResult;
}
else
{
		$allcategorys=$cl->get_category('',"order by createtime desc", "limit $startItem,$ItemNumber");
}



$smarty->assign("categories",$allcategorys);
//handling page Index end

//填充需要信息

$categorysnumber = $cl->get_category_num();
$smarty->assign("categorynumber", $categorysnumber);

$alldevicees = $dl->get_device('',"order by id desc", "");
$smarty->assign("devicees", $alldevicees);
$smarty->assign("menuSelected", "category");
$smarty->assign("projectUrl", constant('PROJECT_URL'));
$smarty->assign("currentUrl","admin/category.php");
$smarty->display("admin/category.html");
?>
